typedef struct{
    char *str;
    int maxLength;
    int length;
} DString;
void Initiate(DString *S, int max, char *string) {
    int i;
    S->str = (char *)malloc(sizeof(char)*max);    //申请动态数组空间
    S->maxLength = max;                           //置动态数组元素最大
    S->length = strlen(string);                   //置串的当前长度值
    for(i = 0; i < S->length; i++)
        S->str[i] = string[i];
}

int Insert(DString *S, int pos, DString T) {
    //在主串S的pos位置插入子串T，插入成功则返回1，失败则返回0
    int i;
    if(pos < 0){
        printf("参数pos出错!");
        return 0;
    }
    else {
        if(S->length + T.length > S->maxLength){
            //重新申请S->str所指数组空间，原数组元素存放在新数组的前面
            realloc(S->str, (S->length+T.length)*sizeof(char));
            S->maxLength = S->length+T.length;
        }
        for(i = S->length-1; i >= pos; i--)
            S->str[i+T.length] = S->str[i]; //依次后移T.length个位置
        for(i = 0; i < T.length; i++)
            S->str[pos+i] = T.str[i];      //插入
        S->length += T.length;             //置新的元素个数
        return 1;
    }
}
int Delete(DString *S, int pos, int len){
    //删除主串S从pos位置开始的长度为len的子串，删除成功则返回1，失败则返回0
    int i;
    if(S->length <= 0){
        printf("数组中未存放字符无元素可删！\n");
        return 0;
    }
    else if(pos < 0 || len < 0 || pos+len > S->length){
        printf("参数pos和len不合法");
        return 0;
    }
    else {
        for(i = pos+len; i <= S->length-1; i++)
            S->str[i-len] = S->str[i];      //依次前移len个位置
        S->length = S->length - len;        //置新的元素个数
        return 1;
    }
}
int SubString(DString *S, int pos, int len, DString *T){
    //取主串S从pos位置开始的长度为len的子串，取成功则返回1，失败则返回0
    int i;
    if(pos < 0 || len < 0 || pos+len > S->length){
        printf("参数pos和len出错!");
        return 0;
    }
    if(len > T->maxLength) {
        T->str = (char *)malloc(len*sizeof(char));  //重新申请数组空间
        T->maxLength = len;
    }
    for(i = 0; i < len; i++)
        T->str[i] = S->str[pos+i];
    T->length = len;
    return 1;
}
void Destroy(DString *S){
    //撤销串S占用的内存空间
    free(S->str);
    S->maxLength = 0;
    S->length = 0;
}